package com.springboot.cloud.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.springboot.cloud.entities.User;
import com.springboot.cloud.service.UserService;

@RestController
public class UserController {

	@Autowired
	private UserService userservice;

	@RequestMapping("/getusername")
	public String showUsername(HttpServletRequest request) {
		SecurityContext context = SecurityContextHolder.getContext();
		// 也可以通过session获得SecurityContext对象
		SecurityContext context_session = (SecurityContext) request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
		// 证明线程绑定的SecurityContext与session域中存储的SecurityContext是同一个
		System.out.println(context == context_session);
		return context.getAuthentication().getName();
	}
	@RequestMapping("/getuserid/{0}")
	public Integer showUserid(@PathVariable("0")String username) {
		return userservice.getUserid(username);
	}
}